<?php

/**
 * 家族
 */
class Api_Family extends PhalApi_Api
{
	
	public function getRules()
	{
		return array(
			'createFamily' => array(
				'uid'           => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token'         => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
				'familyname'    => array('name' => 'familyname', 'type' => 'string', 'desc' => '家族名称'),
				'username'      => array('name' => 'username', 'type' => 'string', 'desc' => '家族长姓名'),
				'cardno'        => array('name' => 'cardno', 'type' => 'string', 'desc' => '身份证号'),
				'divide_family' => array('name' => 'divide_family', 'type' => 'string', 'desc' => '抽成比例'),
				'briefing'      => array('name' => 'briefing', 'type' => 'string', 'desc' => '家族简介'),
				'apply_pos'     => array('name' => 'apply_pos', 'type' => 'string', 'desc' => '身份证正面照'),
				'apply_side'    => array('name' => 'apply_side', 'type' => 'string', 'desc' => '身份证背面照'),
				'apply_map'     => array('name' => 'apply_map', 'type' => 'string', 'desc' => '家族图标'),
			),
		);
	}
	
	
	/**
	 * 创建家族
	 * @desc 用于 创建家族
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[0].switch 开关，0关1开
	 * @return string info[0].type 类型，0图片1视频
	 * @return string info[0].time 图片时间
	 * @return array info[0].list
	 * @return string info[0].list[].thumb 图片、视频链接
	 * @return string info[0].list[].href 页面链接
	 * @return string msg 提示信息
	 */
	public function createFamily()
	{
		$uid           = checkNull($this->uid);
		$token         = checkNull($this->token);
		$familyname    = checkNull($this->familyname);
		$username      = checkNull($this->username);
		$cardno        = checkNull($this->cardno);
		$divide_family = checkNull($this->divide_family);
		$briefing      = checkNull($this->briefing);
		$apply_pos     = checkNull($this->apply_pos);
		$apply_side    = checkNull($this->apply_side);
		$apply_map     = checkNull($this->apply_map);
		$checkToken    = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		if(!$familyname){
			return $this->fail('请填写家族名称！', 1001);
		}
		if(mb_strlen($familyname) > 15){
			return $this->fail('家族名称在15个字符以内！', 1001);
		}
		if(!$username){
			return $this->fail('请填写姓名！', 1002);
		}
		if(mb_strlen($username) > 4){
			return $this->fail('姓名在4个字符以内！', 1002);
		}
		if(!$cardno){
			return $this->fail('请填写身份证号！', 1003);
		}
		
		$cardno_check = isCreditNo($cardno);
		if(!$cardno_check){
			return $this->fail('身份证号不合法！', 1004);
		}
		if(!is_numeric($divide_family)){
			return $this->fail('抽成比例必须是数字！', 1004);
		}
		if($divide_family < 0 || $divide_family > 100 || floor($divide_family) != $divide_family){
			return $this->fail('抽成比例必须是0-100之间的整数！', 1004);
		}
		if(!$briefing){
			return $this->fail('请填写家族简介！', 1005);
		}
		if(mb_strlen($briefing) > 150){
			return $this->fail('家族简介应在150字符以内！', 1005);
		}
		if(!$apply_pos){
			return $this->fail('请上传身份证正面照！', 1006);
		}
		if(!$apply_side){
			return $this->fail('请上传身份证背面照！', 1007);
		}
		if(!$apply_map){
			return $this->fail('请上传家族图标！', 1008);
		}
		
		$info = array(
			'uid'           => $uid,
			'name'          => $familyname,
			'badge'         => $apply_map,
			'apply_pos'     => $apply_pos,
			'apply_side'    => $apply_side,
			'briefing'      => $briefing,
			'carded'        => $cardno,
			'fullname'      => $username,
			'addtime'       => time(),
			'state'         => 0,
			'divide_family' => $divide_family,
		);
		
		$domain = new Domain_Family();
		$res    = $domain->createFamily($info);
		if($res == 1001){
			return $this->fail('家族创建失败！', 1009);
		}
		if($res == 1002){
			return $this->fail('您提交的家族申请正在审核中！', 1010);
		}
		if($res == 1003){
			return $this->fail('家族已创建成功！', 1011);
		}
		
		return $this->success([], '家族创建成功!');
	}
	
	
}
